package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.Purchasing;
import com.my.bean.ResultPage;
import com.my.bean.VoPage;
import com.my.service.PurchasingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 * 每日采购材料 前端控制器
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-08-29
 */
@Controller
@RequestMapping("/purchasing")
public class PurchasingController {

    //注入
    @Autowired
    PurchasingService purchasingService;
    //创建日志
    Logger logger = Logger.getLogger(LossController.class.getName());

    /**
     * 返回页面
     * @return 页面
     */
    @RequestMapping("/arryday")
    public String arryday(){
        return "arryday";
    }


    /**
     * 添加
     * @param
     * @return 添加成功返回true 添加失败返回false
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(String param){
        Purchasing purchasing =JSONObject.parseObject(param,Purchasing.class);
        purchasing.setCreateTime(new Date().getTime());
        if(purchasingService.insert(purchasing)){
            logger.info("添加成功");
            return "添加成功";
        }else{
            logger.info("添加失败");
            return "添加失败";
        }

    }




    /**
     * 查询信息
     * @param voPage 分页
     * @return 返回查询的信息
     */
    @RequestMapping("/select")
    @ResponseBody
    public ResultPage<Purchasing> select(VoPage voPage,String type){
        ResultPage<Purchasing> list = purchasingService.selectMyPage(voPage,type);
        return list;
    }


    /**
     * 删除每日材料购买
     * @param id
     * @return
     */
    @RequestMapping("/deletePurchasing")
    @ResponseBody
    public boolean deletePurchasing(Long id){
        return purchasingService.deleteById(id);
    }
/**
 * 修改
 */
@RequestMapping("/updatePurchasing")
    @ResponseBody
    public String updatePurchasing(Purchasing purchasing){
    List<Purchasing> list = new ArrayList<>();
    list.add(purchasing);
    if(purchasingService.updateBatchById(list)){
        return "修改成功";
    }else{
        return "修改失败";
    }

}

}
